/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bs.persistencia;

import com.bs.util.TelaErro;
import java.io.Serializable;
import java.sql.Connection;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityTransaction;
import javax.persistence.FlushModeType;
import javax.persistence.LockModeType;
import javax.persistence.Query;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;

/**
 *
 * @author Bone
 */
public class EntityManager implements javax.persistence.EntityManager {

    private static SessionFactory factory;
    private Session session;
    private Transaction transaction;
    private FlushModeType modeType = FlushModeType.AUTO;

    public EntityManager() {
        if (java.beans.Beans.isDesignTime()) {
            return;
        }
        if (factory == null) {
            factory = HelperHibernate.getSessionFactory();

        }
        if (factory.isClosed()) {
            HelperHibernate.abrir("", "");
        }
        if (session == null) {
            session = factory.openSession();
            transaction = new Transaction(session.getTransaction());
        }
    }

    @Override
    public void persist(Object o) {
        try {
            session.persist(o);
        } catch (Exception ex) {
            TelaErro.exibeErro(ex);
        }
    }

    @Override
    public <T> T merge(T t) {
        try {
            return (T) session.merge(t);
        } catch (Exception ex) {
            TelaErro.exibeErro(ex);
        }
        return null;
    }

    @Override
    public void remove(Object o) {
        try {
            session.delete(o);
        } catch (Exception ex) {
            TelaErro.exibeErro(ex);
        }
    }

    @Override
    public <T> T find(Class<T> type, Object o) {
        try {
            return (T) session.load(type, (Serializable) o);
        } catch (Exception ex) {
            TelaErro.exibeErro(ex);
        }
        return null;
    }

    @Override
    public <T> T getReference(Class<T> type, Object o) {
        return (T) this;
    }

    @Override
    public void flush() {
        session.flush();
    }

    @Override
    public void setFlushMode(FlushModeType fmt) {
        modeType = fmt;
    }

    @Override
    public FlushModeType getFlushMode() {

        return modeType;
    }

    @Override
    public void lock(Object o, LockModeType lmt) {
        session.lock(o, LockMode.parse(lmt.name()));
    }

    @Override
    public void refresh(Object o) {
        try {
            session.refresh(o);
        } catch (Exception ex) {
            TelaErro.exibeErro(ex);
        }
    }

    @Override
    public void clear() {
        try {
            session.clear();
        } catch (Exception ex) {
            TelaErro.exibeErro(ex);
        }
    }

    @Override
    public boolean contains(Object o) {
        try {
            return session.contains(o);
        } catch (Exception ex) {
            TelaErro.exibeErro(ex);
        }
        return false;
    }

    @Override
    public Query createQuery(String string) {
        try {
            return new com.bs.persistencia.Query(session.createQuery(string));
        } catch (Exception ex) {
            TelaErro.exibeErro(ex);
        }
        return null;
    }

    @Override
    public Query createNamedQuery(String string) {
        try {
            return new com.bs.persistencia.Query(session.getNamedQuery(string));
        } catch (Exception ex) {
            TelaErro.exibeErro(ex);
        }
        return null;
    }

    @Override
    public Query createNativeQuery(String string) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Query createNativeQuery(String string, Class type) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Query createNativeQuery(String string, String string1) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void joinTransaction() {
    }

    @Override
    public Object getDelegate() {
        return factory;
    }

    @Override
    public void close() {
        session.close();
    }

    @Override
    public boolean isOpen() {
        return session.isOpen();
    }

    @Override
    public EntityTransaction getTransaction() {
        try {
            return (EntityTransaction) transaction;
        } catch (Exception ex) {
            TelaErro.exibeErro(ex);
        }
        return null;
    }

    public Connection getConnection() {
        try {
            return session.connection();
        } catch (Exception ex) {
            TelaErro.exibeErro(ex);
        }
        return null;
    }

    public void carregar(Object o, Serializable id) {
        try {
            session.load(o, id);
        } catch (Exception ex) {
            TelaErro.exibeErro(ex);
        }
    }

    public void excluir(Object o) {
        try {
            session.delete(o);
        } catch (Exception ex) {
            TelaErro.exibeErro(ex);
        }
    }

    public void alterar(Object o) {
        try {
            session.update(o);
        } catch (Exception ex) {
            TelaErro.exibeErro(ex);
        }

    }

    public void inserir(Object o) {
        try {
            session.persist(o);
        } catch (Exception ex) {
            TelaErro.exibeErro(ex);
        }
    }

    public List getCollection(Class tipo, Criterion criterion) {
        try {
            Criteria crit = session.createCriteria(tipo);
            crit.add(criterion);
            return crit.list();
        } catch (Exception ex) {
            TelaErro.exibeErro(ex);
        }
        return null;
    }

    public List getCollection(Class tipo, Criterion criterion, int limiteRegistros) {
        try {
            Criteria crit = session.createCriteria(tipo);
            crit.setMaxResults(limiteRegistros);
            crit.add(criterion);
            return crit.list();
        } catch (Exception ex) {
            TelaErro.exibeErro(ex);
            return null;
        }
    }

    public Collection getCollection(Class classe) {
        try {
            return new HelperHibernate().getCollection(classe);
        } catch (Exception ex) {
            TelaErro.exibeErro(ex);
        }
        return null;
    }

    public Collection getCollection(Class classe, int limite) {
        try {
            return session.createCriteria(classe).setMaxResults(limite).list();
        } catch (Exception ex) {
            TelaErro.exibeErro(ex);
        }
        return null;
    }
}
